'user strict';
import React, {
    Component
} from 'react';
import {
    hashHistory,
    Link
} from 'react-router'
import {
    pageNameList,
    urlKey
} from '../../Tools'

export default class Header extends Component {
    constructor(props) {
        super(props)
        this.baiduStatistics()
    }

    componentWillMount() {
        clearInterval(sessionStorage.interval); //清除定时器
        delete sessionStorage.interval
    }

    componentWillUnmount() {
        clearInterval(sessionStorage.interval); //清除定时器
        delete sessionStorage.interval
        window.onscroll = null //解绑滚动事件
    }


    //百度统计
    baiduStatistics() {
        let name = location.hash.slice(2).split('?')[0].split('/')[0].toLocaleLowerCase(),
            fullName = pageNameList[name];

        if (fullName == undefined) {
            name = '未知'
            fullName = '未知'
        }
        if (fullName == '园丁园' && sessionStorage.userrole == '家长')
            fullName = '快乐城堡'
        console.log(fullName);
        _hmt.push(['_trackEvent', 'page', name, fullName]);
    }

    //返回操作
    goBlack() {
        clearInterval(sessionStorage.interval); //清除定时器
        delete sessionStorage.interval
        //执行新方法
        if (this.props.addFn) {
            this.props.addFn();
            return;
        }
        //执行新跳转
        if (this.props.toLink) {
            hashHistory.push(this.props.toLink);
            return;
        }
        if (this.props.outLink) {
            location.href = this.props.outLink
            return;
        }
        //app引用跳转
        if (this.props.appLink) {
            if (sessionStorage.appback == 2) {
                hashHistory.goBack(-1);
                return
            }
            if (sessionStorage.app == 'true' && this.props.appsign == undefined) {
                location.href = '/jiaxiaotong/appback'
                return;
            }
        }
        if (urlKey('appback') == 1) {
            location.href = '/jiaxiaotong/appback'
            return;
        }
        hashHistory.goBack(-1);
    }
    render() {
        const {
            title,
            link,
            noBack
        } = this.props;
        return (
            <header className="header">
                {
                    urlKey('weixinmsg')=='true'?
                    <Link className="h_back home" to='/index' />
                    :noBack||sessionStorage.appback=='0'?'':<a className="h_back" onClick={this.goBlack.bind(this)}></a>
                }
                <label className="h_title">
                    {title
                        ? title
                        : this.props.children}
                </label>
                {link
                    ? link
                    : ''}
            </header>
        )
    }
}
